package servlet.manager;

import entity.Announcement;
import entity.DateTime;
import net.sf.json.JSONObject;
import service.manager.AnnouncementService;
import service.manager.impl.AnnouncementServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * AnnouncementAddServlet
 *
 * @author 谭文斌
 * @since 2023/8/31
 */
@WebServlet("/manager/announcementAddServlet")
public class AnnouncementAddServlet extends HttpServlet {
    AnnouncementService service=new AnnouncementServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf8");
        PrintWriter out=resp.getWriter();
        JSONObject json = new JSONObject();

        String tit = req.getParameter("title");
        String det = req.getParameter("detail");
        DateTime date = new DateTime();
        String time = date.show();
        Announcement announce = new Announcement(tit,det,time);

        int add=service.add(announce);
        if (add==1){
            json.put("code","0");
            json.put("msg", "success");
        }else {
            json.put("code", "1");
            json.put("msg", "error");
        }
        out.println(json.toString());
    }
}
